package com.woniu.supermarket.web;

import com.github.pagehelper.PageInfo;
import com.woniu.supermarket.entity.PurchaseReturn;
import com.woniu.supermarket.entity.PurchaseReturnCheck;
import com.woniu.supermarket.result.ResultResponse;
import com.woniu.supermarket.service.IPurchaseReturnService;
import com.woniu.supermarket.util.CodeCreateUtil;
import com.woniu.supermarket.vo.PurchaseReturnVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("PurchaseReturn")
@Api(value = "采购退货操作")
public class PurchaseReturnController {

    @Autowired
    IPurchaseReturnService iPurchaseReturnService;

    @GetMapping("/showList")
    @ApiOperation(value = "采购退货单分页查询操作")
    public ResultResponse<PageInfo<PurchaseReturnVo>> showList(@ApiParam(name = "now",value = "当前页")@RequestParam(name = "now",defaultValue = "1") Integer now,
                                                               @ApiParam(name = "size",value = "页大小")@RequestParam(name = "size",defaultValue = "3") Integer size,
                                                               @ApiParam(name = "name",value = "接收前台传来的采购退货单号") String name,
                                                               @ApiParam(name = "status",value = "接收状态")@RequestParam(name = "status") Integer status){
        ResultResponse<PageInfo<PurchaseReturnVo>> result = null;
        System.out.println(status);
        try {
            PageInfo<PurchaseReturnVo> pageInfo = iPurchaseReturnService.findByCons(now, size, name,status);
            result = new ResultResponse<>(200,"查询采购退货单成功",pageInfo);
        }catch (RuntimeException e){
            e.printStackTrace();
            result = new ResultResponse<>(201,"查询采购退货单失败");
        }
        return result;
    }


    @GetMapping("/showAll")
    @ApiOperation(value = "采购退货单查询所有操作")
    public ResultResponse<List<PurchaseReturn>> showAll(){
        ResultResponse<List<PurchaseReturn>> result = null;
        try {
            List<PurchaseReturn> lists = iPurchaseReturnService.findAll();
            result = new ResultResponse<>(200,"查询采购退货单成功",lists);
        }catch (RuntimeException e){
            e.printStackTrace();
            result = new ResultResponse<>(201,"查询采购退货单失败");
        }
        return result;
    }


    @PutMapping("/modifyPurchaseReturn")
    @ApiOperation(value = "修改采购退货订单操作")
    public ResultResponse<Void> modifyPurchaseReturn(@RequestBody PurchaseReturnVo purchaseReturnVo){
        ResultResponse<Void> result = null;
        try {
            iPurchaseReturnService.modifyPurchaseReturn(purchaseReturnVo);
            result = new ResultResponse<>(200,"修改采购退货单成功");
        }catch (RuntimeException e){
            e.printStackTrace();
            result = new ResultResponse<>(201,"修改采购退货单失败");
        }
        return result;
    }


    @DeleteMapping("/del/{id}")
    @ApiOperation(value = "删除采购退货订单操作")
    public ResultResponse<Void> del(@PathVariable("id")Integer id){
        ResultResponse<Void> result = null;
        try {
            iPurchaseReturnService.removePurchaseReturnById(id);
            result = new ResultResponse<>(200,"删除采购退货单成功");
        }catch (RuntimeException e){
            e.printStackTrace();
            result = new ResultResponse<>(201,"删除采购退货单失败");
        }
        return result;
    }


    @PostMapping("/addPurchaseReturn")
    @ApiOperation(value = "添加采购退货订单操作")
    public ResultResponse<Void> addPurchaseReturn(@RequestBody PurchaseReturnVo purchaseReturnVo){
        ResultResponse<Void> result = null;
        try {
            iPurchaseReturnService.addPurchaseReturn(purchaseReturnVo);
            result = new ResultResponse<>(200,"添加采购退货单成功");
        }catch (RuntimeException e){
            e.printStackTrace();
            result = new ResultResponse<>(201,"添加采购退货单失败");
        }
        return result;
    }


    @GetMapping("/code")
    @ApiOperation(value = "添加退货单时，先生成编码")
    public ResultResponse<String> code(){
        ResultResponse<String> result = null;
        try {
            String code = CodeCreateUtil.createPurchaseReturnCode();
            result = new ResultResponse<>(200,"查询编码成功",code);
        }catch (RuntimeException e){
            e.printStackTrace();
            result = new ResultResponse<>(201,"查询编码失败");
        }
        return result;
    }




    @PostMapping("/auditPurchaseReturn")
    @ApiOperation(value = "审核退货订单操纵")
    public ResultResponse<PurchaseReturnCheck> auditPurchase(@RequestBody PurchaseReturnCheck purchaseReturnCheck){
        ResultResponse<PurchaseReturnCheck> result = null;
        try {
            iPurchaseReturnService.auditPurchaseReturnCheck(purchaseReturnCheck);
            result = new ResultResponse<>(200,"审核采购退货订单成功",purchaseReturnCheck);
        }catch (RuntimeException e){
            e.printStackTrace();
            result = new ResultResponse<>(201,"审核采购退货订单失败");
        }
        return result;
    }


    @PutMapping("/modifyPurchaseReturn2")
    public ResultResponse<Void> modifyPurchaseReturn2(@RequestBody PurchaseReturnVo purchaseReturnVo){
        ResultResponse<Void> result = null;
        try {
            iPurchaseReturnService.modifyPurchaseReturn(purchaseReturnVo);
            result = new ResultResponse<>(200,"修改采购退货单成功");
        }catch (RuntimeException e){
            e.printStackTrace();
            result = new ResultResponse<>(201,"修改采购退货单失败");
        }
        return result;
    }




    @GetMapping("/doAdvancedSearch")
    @ApiOperation(value = "采购退货单分页查询操作")
    public ResultResponse<PageInfo<PurchaseReturnVo>> doAdvancedSearch(@ApiParam(name = "now",value = "当前页")@RequestParam(name = "now",defaultValue = "1") Integer now,
                                                               @ApiParam(name = "size",value = "页大小")@RequestParam(name = "size",defaultValue = "3") Integer size,
                                                               @ApiParam(name = "name",value = "接收前台传来的采购退货单号") String returnNumber,
                                                               @ApiParam(name = "status",value = "接收状态")@RequestParam(name = "status") Integer status,
                                                               @RequestParam(name = "purchaseId",defaultValue = "0")Integer purchaseId){
        ResultResponse<PageInfo<PurchaseReturnVo>> result = null;
        System.out.println(status);
        try {
            PageInfo<PurchaseReturnVo> pageInfo = iPurchaseReturnService.findByCons1(now, size, returnNumber,status,purchaseId);
            result = new ResultResponse<>(200,"查询采购退货单成功",pageInfo);
        }catch (RuntimeException e){
            e.printStackTrace();
            result = new ResultResponse<>(201,"查询采购退货单失败");
        }
        return result;
    }

}
